-- =========================================================================================
-- 				PROTECT THE VIP - Gamemode by SteveUK
--  You are free to make modifications and redistributions of this gamemode as long as I'm credited for it
--=========================================================================================

-- team id defs
TEAM_ASSASSINS		= 5
TEAM_DEFENDERS		= 6
TEAM_VIP			= 7

-- team setups
team.SetUp( TEAM_ASSASSINS, "Assassins", Color( 204, 0, 0, 255 ) )  
team.SetUp( TEAM_DEFENDERS, "VIP Protection", Color( 67, 156, 228, 255 ) )  
team.SetUp( TEAM_VIP, "VIP", Color( 255, 140, 26, 0 ) )

-- team models
tAssassinModels = { "models/Humans/Group03/male_01.mdl", "models/Humans/Group03/male_02.mdl", "models/Humans/Group03/male_03.mdl" }
tProtectionModels = { "models/Combine_Soldier.mdl", "models/Combine_Soldier_PrisonGuard.mdl", "models/Combine_Super_Soldier.mdl" }
tVIPModels = { "models/breen.mdl" }

-- precache all the team models, to be called in Initalize
function GM:PrecacheModels()

	local tempModelsToPrecache = tAssassinModels -- start table with assassin models
	table.Add( tempModelsToPrecache, tProtectionModels ) -- add protector models
	table.Add( tempModelsToPrecache, tVIPModels ) -- VIP models, this is probably only breen though but this shit makes it more robust
	
	for k, v in pairs( tempModelsToPrecache ) do -- precache model
		util.PrecacheModel( v ) -- add it to precache list
	end
	
end

-- helper function for grabbing a random value from a table
function GM:PickRandomFromTable( t )
	return t[ math.random( 1, #t ) ] 
end

-- function for getting a random model from the list
function GM:GetRandomModel( team )

	if( team == TEAM_ASSASSINS ) then
		return GAMEMODE:PickRandomFromTable( tAssassinModels )
	elseif( team == TEAM_DEFENDERS ) then
		return GAMEMODE:PickRandomFromTable( tProtectionModels )
	elseif( team == TEAM_VIP ) then
		return GAMEMODE:PickRandomFromTable( tVIPModels )
	else -- hopefully we'll never end up here
		return "models/Humans/Group03/male_01.mdl"
	end
	
end

-- pick a random model for a team player when they spawn
function GM:PickTeamModel( pl )
	if( !pl:IsPlayer() ) then return end -- make sure we're dealing with a player
	pl:SetModel( GAMEMODE:GetRandomModel( pl:Team() ) ) -- pick a random model and set it
end

-- helper function for checking if a player is friendly or not (since VIP != VIP Protection ), compares 2 players
function GM:IsAlly( plsrc, plcheck )

	if( plsrc:Team() == plcheck:Team() ) then -- same team
		return true
	elseif( ( plsrc:Team() == TEAM_DEFENDERS and plcheck:Team() == TEAM_VIP ) or ( plcheck:Team() == TEAM_DEFENDERS and plsrc:Team() == TEAM_VIP ) ) then -- VIP and protectors are friends of course
		return true
	else -- definatley not friendly
		return false
	end
	
end

-- these below functions add on to the player object
local TeamPlayer = FindMetaTable( "Player" )

if ( TeamPlayer ) then return end
	
-- whether the player is vip or not
function TeamPlayer:IsVIP()
	
	if( self:Team() == TEAM_VIP ) then
		return true
	else
		return false
	end	

end

-- check whether a pl is an ally of another.
function TeamPlayer:IsAllyOf( otherplayer )
	return GAMEMODE:IsAlly( self, otherplayer )
end
	
